<?php

namespace App\Console\Commands\Tasks\Data;
use App\Repositories\DataSummaryRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;

class ComputeSummaryDaily extends Command
{
    //命令名
    protected $signature = 'task:compute-summary-daily {start_date=0} {end_date=0}';

    //命令描述
    protected $description = '计算每日汇总数据';

    // 业务处理
    public function handle()
    {
        $this->info("运行时间:" . date("Y-m-d H:i:s"));
        try {
            $startDate = $this->argument('start_date');
            if (empty($startDate)) {
                $startDate = Carbon::create()->subDay()->startOfDay();
                $this->info("未输入开始日期，默认为一天前:" . $startDate->toDateString());
            } else {
                if (!Carbon::hasFormat($startDate, "Ymd")) {
                    throw new \Exception("输入的开始日期不合法！");
                }
                $startDate = Carbon::createFromFormat("Ymd", $startDate)->startOfDay();
            }

            $endDate = $this->argument('end_date');
            if (empty($endDate)) {
                $endDate = Carbon::create()->subDay()->endOfDay();
                $this->info("未输入结束日期，默认为前一天:" . $endDate->toDateString());
            } else {
                if (!Carbon::hasFormat($endDate, "Ymd")) {
                    throw new \Exception("输入的结束日期不合法！");
                }
                $endDate = Carbon::createFromFormat("Ymd", $endDate)->endOfDay();
            }

            if ($startDate->greaterThan($endDate)) {
                throw new \Exception("开始时间不能大于结束时间！");
            }

            $this->deal($startDate, $endDate);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function deal($startObj, $endObj) {
        while ($startObj->lessThanOrEqualTo($endObj)) {
            $curObj = $startObj;
            $repo = app("repo_data_summary");
            $registerNumber = $repo->getUserRegisterNumber($curObj);
            $rechargeMoney = $repo->getUserRechargeMoney($curObj);
            $sendGiftValue = $repo->getSendGiftValue($curObj);
            $withdrawMoney = $repo->getWithdrawMoney($curObj);
            $justSendGiftValue = $repo->getJustSendGiftValue($curObj);
            $catCost = $repo->getGameCatCostGoldDaily($curObj);
            $catReward = $repo->getGameCatGiftGoldDaily($curObj);
            $treeCost = $repo->getGameTreeCostGoldDaily($curObj);
            $treeReward = $repo->getGameTreeGiftGoldDaily($curObj);
            $candyCost = $repo->getCandyGoldDaily($curObj);
            $candyReward = $repo->getCandyGoldDaily($curObj);
            $blindBoxCost = $repo->getBlindBoxTotalCostGoldDaily($curObj);
            $blindBoxReward = $repo->getBlindBoxGiftGoldDaily($curObj);
            $repo->insertWithDate([
                "register_user_number" => $registerNumber,
                "has_recharge_money" => $rechargeMoney,
                "send_gift_value" => $sendGiftValue,
                "has_withdraw_money" => $withdrawMoney,
                "just_send_gift_value" => $justSendGiftValue,
                "cat_cost" => $catCost,
                "cat_reward" => $catReward,
                "tree_cost" => $treeCost,
                "tree_reward" => $treeReward,
                "candy_cost" => $candyCost,
                "candy_reward" => $candyReward,
                "blind_box_cost" => $blindBoxCost,
                "blind_box_reward" => $blindBoxReward,
            ], $curObj->format("Y-m-d"));

            $this->info($curObj->toDateString() . "的数据生成完毕！");
            $startObj->addDay();
        }
    }

}